package com.blb.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 懒汉式单例
 */
public class LazySingleton {

    //懒汉式一开始不创建实例
    private static volatile LazySingleton instance = null;

    private LazySingleton(){
        System.out.println("创建懒汉式单例");
    }

    public static LazySingleton getInstance(){
        //外面if判断对象为空再执行同步代码块，提升性能
        if(instance == null) {
            synchronized (LazySingleton.class) {
                //内部if判断对象为空再创建对象
                if (instance == null) {
                    instance = new LazySingleton();
                }
            }
        }
        return instance;
    }
}
